package com.sunxueqi.project.tool.service.impl;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.sunxueqi.common.utils.TemplateUtil;
import com.sunxueqi.project.tool.dao.GenerateMapper;
import com.sunxueqi.project.tool.service.IGenerateService;
import com.sunxueqi.project.tool.vo.BeanField;
import com.sunxueqi.project.tool.vo.GenerateInput;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

@Service
public class GenerateServiceImpl implements IGenerateService{

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	private GenerateMapper generateMapper;
	
	private static Map<String, String> map = Maps.newHashMap();
	static {
		map.put("int", Integer.class.getSimpleName());
		map.put("tinyint", Integer.class.getSimpleName());
		map.put("double", Double.class.getSimpleName());
		map.put("float", Float.class.getSimpleName());
		map.put("decimal", BigDecimal.class.getSimpleName());
		map.put("date", Date.class.getSimpleName());
		map.put("timestamp", Date.class.getSimpleName());
		map.put("datetime", Date.class.getSimpleName());
		map.put("varchar", String.class.getSimpleName());
		map.put("text", String.class.getSimpleName());
		map.put("longtext", String.class.getSimpleName());
	}
	
	private RowMapper<BeanField> beanFieldMapper = new RowMapper<BeanField>() {
		@Override
		public BeanField mapRow(ResultSet rs, int rowNum) throws SQLException {
			BeanField beanField = new BeanField();
			beanField.setColumnName(rs.getString("column_name"));
			beanField.setColumnType(rs.getString("data_type"));
			beanField.setColumnComment(rs.getString("column_comment"));
			beanField.setColumnDefault(rs.getString("column_default"));
			return beanField;
		}
	};
	
	
	
	@Override
	public List<BeanField> listBeanField(String tableName) {
		List<BeanField> beanFields = jdbcTemplate.query("select column_name, data_type, column_comment, column_default FROM information_schema.columns WHERE table_name= ? and table_schema = (select database())",
				new String[] { tableName }, beanFieldMapper);
		if(ObjectUtil.isAllEmpty(beanFields)) {
			// todo 抛出表不存在的异常
			// throw new Exception("表" + tableName + "不存在");
		}
		beanFields.parallelStream().forEach(b -> {
			
			// 将数据表的字段名改为驼峰命名
			b.setName(StrUtil.toCamelCase(b.getColumnName()));
			// 如果ColumnType在map中不存在，则将其设置为String类型
			String type = map.get(b.getColumnType());
			b.setType(type == null ? String.class.getSimpleName() : type);
			
			b.setColumnDefault(b.getColumnDefault() == null ? "" : b.getColumnDefault());
		});
		return beanFields;
	}


	@Override
	public List<String> getTables() {
		return generateMapper.getTables();
	}


	@Override
	public void generateCode(GenerateInput input) {
		TemplateUtil.saveJava(input);
	}



}
